import { addCardApi, deleteCardListApi, editCardApi, fetchCardListApi, fetchCardProportionApi, findCardApi, renewCardApi, viewCardApi } from '@/api/car/monthCardApi';


const actions = {
  // 用于发送请求获取月卡信息列表的action
  async fetchCardListAction(context, params) {
    const { data } = await fetchCardListApi(params);
    context.commit('setCardListMutation', data);
  },
  // 用于发送请求删除月卡的action
  async deleteCardListAction(context, ids) {
    const { msg } = await deleteCardListApi(ids);
    return msg;
  },
  // 用于发送请求获取月卡占有率的action
  async fetchCardProportionAction(context) {
    const { data } = await fetchCardProportionApi();
    context.commit('setCardProportionAction', data);
  },
  //用于发送请求添加月卡的action
  async addCardAction(context, data) {
    await addCardApi(data);
    return '添加月卡成功';
  },
  // 用于发送请求获取月卡详情的action
  async findCardAction(context, id) {
    const { data } = await findCardApi(id);
    return data;
  },
  // 用于发送请求编辑月卡详情的action
  async editCardAction(context, data) {
    await editCardApi(data);
    return '编辑月卡成功';
  },
  // 用于发送请求续费月卡的action
  async renewCardAction(context, data) {
    await renewCardApi(data);
    return '续费月卡成功';
  },
  // 用于发送请求查看月卡详情的action
  async viewCardAction(context, id) {
    const { data } = await viewCardApi(id);
    return data;
  },
};
const mutations = {
  // 用于设置月卡列表的mutation
  setCardListMutation(state, { rows, total }) {
    state.total = total;
    state.cardList = rows;
  },
  // 用于设置月卡占有率的mutation
  setCardProportionAction(state, cardProportion) {
    state.cardProportion = cardProportion;
  },
};

const state = () => {
  return {
    total: 0,
    cardList: [],
    cardProportion: {},
  };

};

export default {
  actions,
  mutations,
  state,
  namespaced: true,
};
